<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
App::uses('Sanitize', 'Utility');

class DemoController extends AppController {

    public $components = array('Acl', 'Link',"Captcha");
    public function initDB() {
        // khong chay ham nay
        try {
            $this -> loadModel('User');
            $group = $this -> User -> Group;

            // Allow admins to everything
            $group -> id = 1;
            $this -> Acl -> allow($group, 'controllers');

            // allow managers to posts and widgets
            $group -> id = 2;
            $this -> Acl -> allow($group, 'controllers');
            $this -> Acl -> deny($group, 'controllers/Users');
            $this -> Acl -> deny($group, 'controllers/Os');

            // allow users to only add and edit on posts and widgets
            $group -> id = 3;
            $this -> Acl -> allow($group, 'controllers');
            $this -> Acl -> deny($group, 'controllers/Users');
            $this -> Acl -> deny($group, 'controllers/Products');
            // allow basic users to log out
            $this -> Acl -> allow($group, 'controllers/Login');
        } catch(Exception $e) {
            echo "<pre>";
            print_r($e);
        }

        // we add an exit to avoid an ugly "missing views" error message
        echo "all done";
        exit ;
    }
    public function testFindAll(){
        if ( $this->request->is('Post') )
		    {
		         $captcha = $this->Session->read('captcha_code');
			if ( $captcha == $this->request->data['User']['captcha'] )
			{
			            $this->Session->setFlash(__('Verification successful'));
			}else{
			    $this->Session->setFlash(__('Captcha code does not match. Verification unsuccessful'));
			}
			$this->redirect(array('action' => 'testFindAll'));
	    }
		
	
    }
	public function get_captcha()
	{
	    $this->autoRender = false;
	    App::import('Component','Captcha');
	    //generate random charcters for captcha
	    $random = mt_rand(100, 99999);	
	    //save characters in session
	    $this->Session->write('captcha_code', $random);  
	    $settings = array(
	         'characters' => $random,  
	  'winHeight' => 250,         // captcha image height
	  'winWidth' => 220,	   // captcha image width
	  'fontSize' => 25,          // captcha image characters fontsize
	  'fontPath' => WWW_ROOT.'fonts/Tahoma.ttf',    // captcha image font
	  'noiseColor' => '#ccc',
	  'bgColor' => '#fff',
	  'noiseLevel' => '100',
	  'textColor' => '#000'
	    );
	    $img = $this->Captcha->ShowImage($settings);
	    echo $img;
	}
	public function updateShortLinkRootCat() {
		$this->loadModel('RootCategory');
		$data = $this->RootCategory->find('all');
		$n = count($data);
		for($i = 0; $i < $n ; $i++) {
			$data[$i]['RootCategory']['short_link'] = $this->Link->getShortForCatagoryLink($data[$i]['RootCategory']['category_name']);
		}
		$this->RootCategory->saveMany($data);
		echo "<pre>";
		print_r($data);exit;
		
	}
	public function updateShortLinkCat() {
		$this->loadModel('Category');
		$data = $this->Category->findAllData();
		$n = count($data);
		for($i = 0; $i < $n ; $i++) {
			$data[$i]['Category']['short_link'] = $this->Link->getShortForCatagoryLink($data[$i]['Category']['category_name'], ".html");
		}
		$this->Category->saveMany($data);
		echo "<pre>";
		print_r($data);exit;
		
	}
	public function testmail(){
		try{
			$Email = new CakeEmail();
			$Email->config('default');
			$Email->emailFormat('html');
			/*sử dụng template để gửi nội dung có html : view/emails/html*/
	        $Email->template('welcome',null)->viewVars( array('test' => "fdsfsdfsdf"));
			$Email->from(array('hotro@maytot.vn' => 'May tot01'))
			    ->to('justinquoctran@gmail.com')
				//->cc('tuanvu101289@gmail.com')
			    ->subject('test02')
			    ->send();
		}
		catch(exception $e){
			echo "<pre>";
			print_r($e);
		}
		exit;
	}
	public function test(){
		$data = $this->request->query['aa'];
		$data = Sanitize::html($data);
		$data = Sanitize::stripScripts($data);
		$data = Sanitize::stripTags($data);
		$data = Sanitize::escape($data);
		
		echo $data;
		exit;
	}
	

}
?>